package com.wutoon.etl.util;

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 武兴云/72176468
 * @version 1.0
 * @date 2023/11/13 14:17
 * @description 日期转换
 */
@Slf4j
public class DateUtil {

    public static String YYYYMMDD = "yyyyMMdd";

    public static String MMDDHHMM = "MMddHHmm";

    public static String MMDD = "MMdd";

    public static String SIMPLE_DATE_FORMATE = "yyyy-MM-dd HH:mm:ss";

    /**
     * 转换当前日期为long
     *
     * @return yyyyMMdd
     * @author 武兴云/72176468
     * @date 2023-11-13 14:20
     * @update_by 武兴云/72176468
     * @update_at 2023-11-13 14:20
     * @creed if you have doubt , please contact me !!!
     */
    public static Long generateCurrentDayId(String format) {
        LocalDateTime currentDate = LocalDateTime.now();
        return generateDayId(currentDate, format);
    }

    /**
     * 转换指定日期为long
     *
     * @return yyyyMMdd
     * @author 武兴云/72176468
     * @date 2023-11-13 14:20
     * @update_by 武兴云/72176468
     * @update_at 2023-11-13 14:20
     * @creed if you have doubt , please contact me !!!
     */
    public static Long generateDayId(LocalDateTime dateTime, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        String formattedDate = dateTime.format(formatter);
        return Long.parseLong(formattedDate);
    }

    /**
     * 根据开始时间，结束时间，获得两个时间之间的所有天，包含两个端点
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @author 武兴云/72176468
     * @date 2023-11-14 16:17
     * @update_by 武兴云/72176468
     * @update_at 2023-11-14 16:17
     * @creed if you have doubt , please contact me !!!
     */
    public static List<Integer> generateDayList(LocalDateTime startTime, LocalDateTime endTime) {
        List<Integer> dayList = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDD);

        LocalDate startDate = startTime.toLocalDate();
        LocalDate endDate = endTime.toLocalDate();

        while (startDate.isBefore(endDate) || startDate.isEqual(endDate)) {
            dayList.add(Integer.parseInt(formatter.format(startDate)));
            startDate = startDate.plusDays(1L);
        }
        return dayList;
    }

}
